//
//  SelectCaseController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/24.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "SelectCaseController.h"
#import "DiseaseDTO.h"

@interface SelectCaseController () <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) NSArray *diseaseDtoArray;

@property (nonatomic, strong) NSMutableArray *result_diseaseDtoArray;

@property (nonatomic, strong) UITextField *textFiled; //搜索textfield
@property (nonatomic, assign) BOOL isTextFiledEnable; //是否有搜素能力
@property (nonatomic, assign) BOOL isSearchState;  //是否在搜索状态

@end

@implementation SelectCaseController



static NSString *ID = @"diseaseCell";

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
    [self loadData];
}



- (void)setupUI {
    
    [self.view addSubview:self.tableview];
    
    self.navigationItem.titleView = self.textFiled;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledTextChange:) name:UITextFieldTextDidChangeNotification object:nil];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.right.bottom.mas_equalTo(0);
    }];
}

- (void)loadData{
    
    WEAKSELF
    [[NetWorkManager sharedManager] loadDiseaseSubWithCi_id:1 page:15 successBlock:^(NSArray *dtoArray) {
        
        weakSelf.diseaseDtoArray = dtoArray;
        
        [weakSelf.tableview reloadData];
        
    } errorBlock:^(NSString *error) {
        
        [SVPAlertManager alertErrorStatus:error];
    }];
}

- (void)textFiledTextChange:(NSNotification *)note{

    NSString *string =  ((UITextField *)note.object).text;
    
    if (self.result_diseaseDtoArray.count>0) {
        
        [self.result_diseaseDtoArray removeAllObjects];
    }
    
    //开始搜索
    NSString *key = string.lowercaseString;
    NSMutableArray *tempArr = [NSMutableArray array];
    
    
    if (![key isEqualToString:@""] && ![key isEqual:[NSNull null]] && key != nil) {
        
        [self.diseaseDtoArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            DiseaseDTO *dto = self.diseaseDtoArray[idx];
            
            //担心框架有时候会误转，再次都设置转为小写
            NSString *name = dto.ci3_name.lowercaseString;
            NSString *namePinyin = dto.ci3_name_pinyin.lowercaseString;
            NSString *nameFireLetter = dto.ci3_name_firstLetter.lowercaseString;
            
            NSRange rang1 = [name rangeOfString:key];
            if (rang1.length>0) { ///比嫩 -比
                
                [tempArr addObject:dto];
            }else{
                
                if ([nameFireLetter containsString:key]) { //bn - b
                    
                    [tempArr addObject:dto];
                    
                }else{  //ershou
                    
                    if ([nameFireLetter containsString:[key substringToIndex:1]]) {
                        
                        if ([namePinyin containsString:key] ) {
                            [tempArr addObject:dto];
                        }
                    }
                }
            }
        }];
        
        [tempArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if (![self.result_diseaseDtoArray containsObject:tempArr[idx]]) {
                
                [self.result_diseaseDtoArray addObject:tempArr[idx]];
            }
        }];
        
        //NSLog(@"self.searchResultList  = %@",self.seachUserDTOList);
        self.isSearchState = YES;
    }else{
        
        self.isSearchState = NO;
    }
    
    [self.tableview reloadData];

}



#pragma mark  - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.isSearchState) {
        
        return self.result_diseaseDtoArray.count;
    }else{
    
        return self.diseaseDtoArray.count;
    }
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    if (self.isSearchState) {
        
        DiseaseDTO *dto = self.result_diseaseDtoArray[indexPath.row];
        
        cell.textLabel.text = dto.ci3_name;
        
    }else{
        
        DiseaseDTO *dto = self.diseaseDtoArray[indexPath.row];
        
        cell.textLabel.text = dto.ci3_name;
    }
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [self.textFiled resignFirstResponder];

    
    NSInteger row = indexPath.row;
    
    NSString *string = nil;
    
    if (self.isSearchState) {

        DiseaseDTO *dto = self.result_diseaseDtoArray[row];
        
        string = dto.ci3_name;

    }else{
        DiseaseDTO *dto = self.diseaseDtoArray[row];
        
        string = dto.ci3_name;
    }
    if (self.clickSelectCaseVCBlock) {
        
        self.clickSelectCaseVCBlock(string);
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark  - Lazy
-(UITableView *)tableview{
    
    if (_tableview == nil) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.tableFooterView = [UIView new];
        [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
        
        }
    return _tableview;
}


-(UITextField *)textFiled{
    
    if (_textFiled == nil) {
        _textFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
        _textFiled.borderStyle = UITextBorderStyleRoundedRect;
        _textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textFiled.placeholder = @"输入关键字";
        _textFiled.backgroundColor = WHITE_COLOR;
        _textFiled.delegate = self;
    }
    return _textFiled;
}



-(NSMutableArray *)result_diseaseDtoArray{
    
    if (_result_diseaseDtoArray == nil) {
        _result_diseaseDtoArray = [[NSMutableArray alloc] init];
    }
    return _result_diseaseDtoArray;
}



@end
